package com.itheima.web.controller;

import cn.hutool.captcha.LineCaptcha;
import com.itheima.domain.db.Admin;
import com.itheima.web.manager.AdminManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.Response;
import java.io.IOException;
import java.util.Map;

@RestController
public class AdminController {

    @Autowired
    private AdminManager adminManager;
    //用户登录验证码图片  void是因为 我们要返回一个图片数据
    @GetMapping("/system/users/verification")
    public void verification(String uuid , HttpServletResponse response) throws IOException {
        //1.调用manager
        LineCaptcha lineCaptcha  = adminManager.verification(uuid);

        //2.将图片返回给浏览器
        lineCaptcha.write( response.getOutputStream() );
    }


    //用户登录
    @PostMapping("/system/users/login")
    public ResponseEntity login(@RequestBody Map<String , String> map) throws IOException {
        //获得数据
        String username = map.get("username");
        String password = map.get("password");
        String verificationCode = map.get("verificationCode");
        String uuid = map.get("uuid");
        return adminManager.login(username ,password , verificationCode ,uuid);
    }
    //用户基本信息
    @PostMapping("/system/users/profile")
    public ResponseEntity profile(@RequestHeader("Authorization") String token) throws IOException {
        Admin admin = adminManager.profile(token);
        return ResponseEntity.ok(admin);
    }

    //用户登出
    @PostMapping("/system/users/logout")
    public ResponseEntity logout(@RequestHeader("Authorization") String token) throws IOException {

        return adminManager.logout(token);
    }


    //概要统计信息
    @GetMapping("/dashboard/summary")
    public ResponseEntity summary() throws IOException {

        return adminManager.summary();
    }


}
